package org.gap.jseed.util;

import java.lang.reflect.Field;

import org.gap.jseed.NoSuchInvocationHandlerError;

public class FieldExtractor {
	public static Object getPropertyFromField(Object object, String fieldName) {
		Object result = tryGetProperty(object, fieldName);
		return result;
	}

	private static Object tryGetProperty(Object object, String fieldName) throws NoSuchInvocationHandlerError {
		try {
			return getDeclaredFieldPropertyOn(object, fieldName);			
		} catch (SecurityException e) {
			throw new NoSuchInvocationHandlerError("This class does not declare a the invocation " + fieldName, e);
		} catch (NoSuchFieldException e) {
			throw new NoSuchInvocationHandlerError("This class does not declare a the invocation " + fieldName, e);
		} catch (IllegalArgumentException e) {
			throw new NoSuchInvocationHandlerError("This class does not declare a the invocation " + fieldName, e);
		} catch (IllegalAccessException e) {
			throw new NoSuchInvocationHandlerError("This class does not declare a the invocation " + fieldName, e);
		}
	}

	private static Object getDeclaredFieldPropertyOn(Object object, String fieldName)
			throws NoSuchFieldException, IllegalAccessException {
		Field field = object.getClass().getDeclaredField(fieldName);
		field.setAccessible(true);
		return field.get(object);
	}
}
